console.log('data_tool content.js');

function getJson() {
    var data = [];
    $('.table tbody tr').each((index, tr) => {
        // console.log(tr);
        let tds = $(tr).find('td');
        // console.log(tds[0]);
        var year = $(tds[0]).text().trim();
        // console.log('year', year, isNaN(year));
        if (isNaN(year))
            return;

        var gdp = $(tds[1]).text().trim();
        gdp = gdp.replace(/,/g, '');
        gdp = gdp.substring(gdp.indexOf('(') + 1, gdp.indexOf(')'));
        var gravity = $(tds[2]).text().trim();
        gravity = gravity.replace(/%/g, '');

        data.push({year, gdp, gravity});
    });
    // console.log('data', data);
    data.reverse();
    // console.log(JSON.stringify(data));
    return data;
}

// 监听来自popup的消息
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
        if (request.action === "getDOM") {
            // 发送网页的DOM内容给popup
            console.log('$(\'title\').text()', $('title').text());
            var title = $('title').text().replace('历年GDP数据 -- 快易数据', '');
            sendResponse({
                domContent: document.body.innerHTML,
                jsonData: {
                    title,
                    data: getJson()
                }
            });
        }
    }
);
